﻿using LxBasic;

namespace LxCore
{
    public partial class StateAndScheduler
    {
        private readonly object _LocalLock = new object();
        private RunningStates _RunningState = RunningStates.Idle;
        public RunningStates RunningState { get => RunningState; }
        public bool IsInterrupting { get => _RunningState == RunningStates.InterruptingByUser || _RunningState == RunningStates.InterruptingByError; }
        public string Description { get; private set; } = string.Empty;
   
        public bool SetRunningState(RunningStates runningState)
        {
            lock (_LocalLock)
            {
                //Switch中挑选false的情况
                switch (runningState)
                {
                    case RunningStates.Running:
                        if (_RunningState != RunningStates.Idle)
                        {
                            return false;
                        }
                        break;
                    case RunningStates.InterruptingByError:
                    case RunningStates.InterruptingByUser:
                        if (_RunningState != RunningStates.Running)
                        {
                            return false;
                        }
                        break;
                }
                _RunningState = runningState;
                return true;
            }
        }
    }
}
//private Action _DeferAction = null;
//public bool TryRequestFlag()
//{
//    lock (_LocalLock)
//    {
//        if (_RunningState == RunningStates.Idle)
//        {
//            _RunningState = RunningStates.Running;
//            return true;
//        }
//        else
//        {
//            LxLog.Error("系统正忙。。。");
//            return false;
//        }
//    }
//}
//public void ReleaseFlag()
//{
//    lock (_LocalLock)
//    {
//        _RunningState = RunningStates.Idle;
//        //runDeferAction_noLock();
//    }
//}
//public void WaitStop()
//{

//}
//private bool isRunning()
//{
//    return _RunningState != RunningStates.Idle;
//}
//public bool GetIsNeedInterrupt()
//{
//    lock (_LocalLock)
//    {
//        return _RunningState == RunningStates.InterruptingByUser || _RunningState == RunningStates.InterruptingByError;
//    }
//}
//public void SetInterrupt_ByUser()
//{
//    lock (_LocalLock)
//    {
//        if (_RunningState == RunningStates.Running)
//        {
//            _RunningState = RunningStates.InterruptingByUser;
//        }
//    }
//}
//public void SetInterrupt_FindError()
//{
//    lock (_LocalLock)
//    {
//        if (_RunningState == RunningStates.Running)
//        {
//            _RunningState = RunningStates.InterruptingByError;
//        }
//    }
//}
//public bool GetIsSettingChanged()
//{
//    lock (_LocalLock)
//    {
//        return _DeferAction != null;
//    }
//}
//public void AddSetAction(Action action)
//{
//    lock (_LocalLock)
//    {
//        if (isRunning())
//        {
//            _DeferAction += action;
//        }
//        else
//        {
//            action.Invoke();
//        }
//    }
//}
//public bool RunDeferAction()
//{
//    lock (_LocalLock)
//    {
//        return runDeferAction_noLock();
//    }
//}
//private bool runDeferAction_noLock()
//{
//    if (_DeferAction == null)
//    {
//        return false;
//    }
//    else
//    {
//        _DeferAction.Invoke();
//        _DeferAction = null;
//        return true;
//    }
//}
//        public void RunCalAction(Action hostAction)
//        {
//            lock (_LocalLock)
//            {
//                if (this.isRunning())
//                {
//                    LxLog.Error("已有执行中的任务，已退出");
//                    return;
//                }
//                _RunningState = RunningStates.Running;
//            }
//#if FrontThreadTest
//            MultiThreads.SetThreadsCount(1);
//            hostAction.Invoke(); 
//            runDeferAction_noLock();
//            _RunningState = RunningStates.Idle;
//#else
//            Thread t = new(() =>
//              {
//                  //try
//                  //{
//                  hostAction.Invoke();
//                  //}
//                  //catch (Exception e)
//                  //{

//                  //}
//                  //finally
//                  //{
//                  lock (_LocalLock)
//                  {
//                      //runDeferAction_noLock();
//                      _RunningState = RunningStates.Idle;
//                  }
//                  //}
//              })
//            { IsBackground = true };
//            t.Start();
//#endif

//        }